import axios from 'axios'
import { MessageBox, Notification } from 'element-ui'
let mess;
const service = axios.create({
  // baseURL: process.env.VUE_APP_BASE_API,
  // baseURL: 'http://localhost:9999',
  baseURL: 'http://tianguoyuan.top:9999',
  timeout: 50000
})
service.defaults.withCredentials = false;
service.interceptors.request.use(
  config => {
    return config
  },
  error => {
    console.log(error, "request --- error");

    return Promise.reject(error, "request")
  }
)
service.interceptors.response.use(
  response => {
    mess ? mess.close() : ''
    const res = response.data
    if (res.code !== 200) {
      if (res.success) {
        return res
      }
      Notification({
        title: "提示",
        message: res.message || res.msg || '请稍后再试',
        type: "warning",
        duration: 1500,
      })
      return Promise.reject(res, "response -- success")
    }
    return res
  },
  error => {
    console.log(error, "response --- error");
    mess ? mess.close() : ''
    if (error.toString().indexOf('timeout') !== -1) {
      MessageBox.confirm('网络连接错误,是否重试？', '提示', {
        distinguishCancelAndClose: true,
        confirmButtonText: '是',
        cancelButtonText: '否'
      }).then(() => {
        window.location.reload()
      }).catch(() => {
        Promise.reject("请稍后再试", error)
      })
      return
    }
    Notification({
      title: "提示",
      message: "系统错误,请稍后再试。",
      type: "error",
      duration: 3000,
    })
    return Promise.reject(error, "response -- error")
  }
)
export default service;